package com.software.frontend.mine.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.software.frontend.R;
import com.software.frontend.mine.adapters.CommentsAdapter;
import com.software.frontend.mine.adapters.FansAdapter;
import com.software.frontend.mine.adapters.LikesAdapter;
import com.software.frontend.mine.adapters.SystemNoticeAdapter;
import com.software.frontend.mine.entity.Comment;
import com.software.frontend.mine.entity.Fans;
import com.software.frontend.mine.entity.Like;
import com.software.frontend.mine.entity.SystemNotice;
import com.software.frontend.mine.utils.DataUtils;

import java.util.ArrayList;

public class NoticeDetails extends AppCompatActivity {

    ListView listView;
    TextView likes, comments, system, fans;
    LinearLayout likesLinear, commentsLinear, systemLinear, fansLinear;
    LikesAdapter likesAdapter;
    CommentsAdapter commentsAdapter;
    SystemNoticeAdapter systemNoticeAdapter;
    FansAdapter fansAdapter;
    ArrayList<Like> likeData;
    ArrayList<Comment> commentsData;
    ArrayList<SystemNotice> systemNoticeData;
    ArrayList<Fans> fansData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_details);

        bindingViews();

        initAdapters();

        setListeners();
    }

    private void bindingViews() {
        listView = (ListView) findViewById(R.id.notice_list);
        likes = (TextView) findViewById(R.id.notices_button_likes);
        comments = (TextView) findViewById(R.id.notices_button_comment);
        system = (TextView) findViewById(R.id.notices_button_system);
        fans = (TextView) findViewById(R.id.notices_button_fans);
        likesLinear = (LinearLayout) findViewById(R.id.notices_button_likes_linear);
        commentsLinear = (LinearLayout) findViewById(R.id.notices_button_comment_linear);
        systemLinear = (LinearLayout) findViewById(R.id.notices_button_system_linear);
        fansLinear = (LinearLayout) findViewById(R.id.notices_button_fans_linear);
    }

    private void initAdapters() {

        //从后端获取数据
        getDataFromService();

        likesAdapter = new LikesAdapter(this, likeData);
        commentsAdapter = new CommentsAdapter(this, commentsData);
        systemNoticeAdapter = new SystemNoticeAdapter(this, systemNoticeData);
        fansAdapter = new FansAdapter(this, fansData);

        listView.setAdapter(likesAdapter);
    }

    //从后端获取//////////////////////////////
    private void getDataFromService() {

        likeData = DataUtils.getLikesNotices(this);
        commentsData = DataUtils.getCommentNotices(this);
        systemNoticeData = DataUtils.getSystemNotices(this);
        fansData = DataUtils.getFansNotices(this);

    }

    private void setListeners() {
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selected = getResources().getColor(R.color.selected);
                switch (v.getId()) {
                    case R.id.notices_button_likes_linear:
                        setSelectedColor();
                        likes.setTextColor(selected);
                        if (likesAdapter != null) {
                            listView.setAdapter(likesAdapter);
                        }
                        break;
                    case R.id.notices_button_comment_linear:
                        setSelectedColor();
                        comments.setTextColor(selected);
                        if (likesAdapter != null) {
                            listView.setAdapter(commentsAdapter);
                        }
                        break;
                    case R.id.notices_button_system_linear:
                        setSelectedColor();
                        system.setTextColor(selected);
                        if (systemNoticeAdapter != null) {
                            listView.setAdapter(systemNoticeAdapter);
                        }
                        break;
                    case R.id.notices_button_fans_linear:
                        setSelectedColor();
                        fans.setTextColor(selected);
                        if (fansAdapter != null) {
                            listView.setAdapter(fansAdapter);
                        }
                        break;
                }
            }
        };
        likesLinear.setOnClickListener(onClickListener);
        commentsLinear.setOnClickListener(onClickListener);
        systemLinear.setOnClickListener(onClickListener);
        fansLinear.setOnClickListener(onClickListener);

    }

    private void setSelectedColor() {
        int white = getResources().getColor(R.color.white);
        likes.setTextColor(white);
        comments.setTextColor(white);
        system.setTextColor(white);
        fans.setTextColor(white);
    }


}